_realname=qemu
pkgbase=mingw-w64-${_realname}
pkgname=(
  "${MINGW_PACKAGE_PREFIX}-qemu"
  "${MINGW_PACKAGE_PREFIX}-qemu-common"
  "${MINGW_PACKAGE_PREFIX}-qemu-guest-agent"
  "${MINGW_PACKAGE_PREFIX}-qemu-image-util"
)
_base_ver="10.0.0"
# QEMU Versioning of RC-SourcePackage and RC-Version differs
# e.g. qemu-6.1.0-rc0.tar.xz contains 6.0.90, qemu-6.1.0-rc1.tar.xz contains 6.0.91
# Unset _rc_no to create Release-Build
#_rc_no="4"
_rc_base_ver="9.2.9${_rc_no}"
_rc_file_ver="rc${_rc_no}"
_tarname=$( [ -z "${_rc_no}" ] && echo ${_realname}-${_base_ver} || echo ${_realname}-${_base_ver}-${_rc_file_ver} )
pkgver=$( [ -z "${_rc_no}" ] && echo ${_base_ver} || echo ${_rc_base_ver} )
pkgrel=2
pkgdesc="QEMU - a generic and open source machine emulator and virtualizer (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('spdx:GPL-2.0-or-later AND LGPL-2.1-or-later AND BSD-2-Clause AND BSD-3-Clause AND MIT')
url="https://qemu.org/"
msys2_repository_url="https://gitlab.com/qemu-project/qemu"
msys2_references=(
  'archlinux: qemu'
  'cpe: cpe:/a:qemu:qemu'
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-python-sphinx"
  "${MINGW_PACKAGE_PREFIX}-python-sphinx_rtd_theme"
  "${MINGW_PACKAGE_PREFIX}-tools-git")
case ${MSYSTEM} in
  CLANG*)
    # work around lack of windmc in llvm
    makedepends+=("binutils")
      ;;
esac
depends=(
  "${MINGW_PACKAGE_PREFIX}-angleproject"
  "${MINGW_PACKAGE_PREFIX}-capstone"
  "${MINGW_PACKAGE_PREFIX}-curl"
  "${MINGW_PACKAGE_PREFIX}-cyrus-sasl"
  "${MINGW_PACKAGE_PREFIX}-dtc"
  "${MINGW_PACKAGE_PREFIX}-expat"
  "${MINGW_PACKAGE_PREFIX}-fontconfig"
  "${MINGW_PACKAGE_PREFIX}-freetype"
  "${MINGW_PACKAGE_PREFIX}-fribidi"
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
  "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
  "${MINGW_PACKAGE_PREFIX}-gettext"
  "${MINGW_PACKAGE_PREFIX}-glib2"
  "${MINGW_PACKAGE_PREFIX}-gmp"
  "${MINGW_PACKAGE_PREFIX}-gnutls"
  "${MINGW_PACKAGE_PREFIX}-graphite2"
  "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
  "${MINGW_PACKAGE_PREFIX}-gstreamer"
  "${MINGW_PACKAGE_PREFIX}-gtk3"
  "${MINGW_PACKAGE_PREFIX}-harfbuzz"
  "${MINGW_PACKAGE_PREFIX}-jbigkit"
  "${MINGW_PACKAGE_PREFIX}-lerc"
  "${MINGW_PACKAGE_PREFIX}-libc++"
  "${MINGW_PACKAGE_PREFIX}-libdatrie"
  "${MINGW_PACKAGE_PREFIX}-libdeflate"
  "${MINGW_PACKAGE_PREFIX}-libepoxy"
  "${MINGW_PACKAGE_PREFIX}-libffi"
  "${MINGW_PACKAGE_PREFIX}-libiconv"
  "${MINGW_PACKAGE_PREFIX}-libidn2"
  "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
  "${MINGW_PACKAGE_PREFIX}-libnfs"
  "${MINGW_PACKAGE_PREFIX}-libpng"
  "${MINGW_PACKAGE_PREFIX}-libpsl"
  "${MINGW_PACKAGE_PREFIX}-libslirp"
  "${MINGW_PACKAGE_PREFIX}-libssh"
  "${MINGW_PACKAGE_PREFIX}-libssh2"
  "${MINGW_PACKAGE_PREFIX}-libtasn1"
  "${MINGW_PACKAGE_PREFIX}-libthai"
  "${MINGW_PACKAGE_PREFIX}-libtiff"
  "${MINGW_PACKAGE_PREFIX}-libunistring"
  "${MINGW_PACKAGE_PREFIX}-libunwind"
  "${MINGW_PACKAGE_PREFIX}-libusb"
  "${MINGW_PACKAGE_PREFIX}-libwebp"
  "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
  "${MINGW_PACKAGE_PREFIX}-lz4"
  "${MINGW_PACKAGE_PREFIX}-lzo2"
  "${MINGW_PACKAGE_PREFIX}-nettle"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-opus"
  "${MINGW_PACKAGE_PREFIX}-orc"
  "${MINGW_PACKAGE_PREFIX}-p11-kit"
  "${MINGW_PACKAGE_PREFIX}-pango"
  "${MINGW_PACKAGE_PREFIX}-pixman"
  "${MINGW_PACKAGE_PREFIX}-SDL2"
  "${MINGW_PACKAGE_PREFIX}-SDL2_image"
  "${MINGW_PACKAGE_PREFIX}-snappy"
  "${MINGW_PACKAGE_PREFIX}-spice"
  "${MINGW_PACKAGE_PREFIX}-usbredir"
  $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-virglrenderer")
  "${MINGW_PACKAGE_PREFIX}-xz"
  "${MINGW_PACKAGE_PREFIX}-zlib"
  "${MINGW_PACKAGE_PREFIX}-zstd"
)

options=('!emptydirs')
source=(
  "https://download.qemu.org/${_tarname}.tar.xz"{,.sig}
  msys2.readme.txt
  msys2.qemu-guest-agent.txt
  msys2.examples.tests.sh
)
sha256sums=(
  '22c075601fdcf8c7b2671a839ebdcef1d4f2973eb6735254fd2e1bd0f30b3896'
  'SKIP'
  '51625fd83c0a63729942d3dfc6d48be3491a700fe0f7cb8e88935b46081c9016'
  'f2f9eeb31023d002f54637a9941ea0d8fae3c6f0a66c05c033857b305bd1470d'
  'a2db7f1d696a8609df54f48f20760ef100b04b39d2bce1ca0a7989375afab8a1'
)
validpgpkeys=('CEACC9E15534EBABB82D3FA03353C9CEF108B584') # Michael Roth <flukshun@gmail.com>
# tar cannot create links, to keep build running, manual extraction is required
noextract=(${_tarname}.tar.xz)

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  [[ -d "${srcdir}"/${_tarname} ]] && rm -rf "${srcdir}"/${_tarname}
  # tar cannot create links here, therefore hide the failure
  tar -xf "${srcdir}"/${_tarname}.tar.xz -C "${srcdir}" || true

  cd "${srcdir}"/${_tarname}
  # apply_patch_with_msg
}

build() {
  mkdir -pv "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  # configure fails to create links, which can be ignored
  # qemu enables all features if possible, so keep it simple
  # For faster testing:
  #CONFIGURE_OPTS="--target-list=x86_64-softmmu"
  ../${_tarname}/configure $CONFIGURE_OPTS \
    --prefix=${MINGW_PREFIX} \
    --bindir=bin \
    --datadir=share/qemu \
    --localedir=share/locale \
    --mandir=share/man \
    --docdir=share/doc/qemu

  # Finally build
  make
  # TODO Remove widl-cmd as soon as qga-vss.tlb is generated by make
  cd qga/vss-win32
  widl -I${MINGW_PREFIX}/include -L${MINGW_PREFIX}/include \
    -t "${srcdir}"/${_tarname}/qga/vss-win32/qga-vss.idl -o qga-vss.tlb
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  # Tests are not performed in CI
  # For local testing: perform, but do not fail
  make test || true
}

makeinstall() {
  cd "${srcdir}"/build-${MSYSTEM}

  # Install to "${pkgdir}"
  make DESTDIR="${pkgdir}" install

  # ${MINGW_PREFIX} was installed somewhere below "${pkgdir}", determine ...
  local P_INSTALL=$(find "${pkgdir}" -name "$(basename ${MINGW_PREFIX})" -type d)
  # Move directly to "${pkgdir}", if not already there
  [ -d "${pkgdir}"/${MINGW_PREFIX} ] || mv -v "$P_INSTALL" "${pkgdir}"/

  # To enable usage relative paths for next operations
  cd "${pkgdir}"/${MINGW_PREFIX}

  # Move content to standard dirs in share/
  mkdir -pv share/qemu/icons
  mv -v share/qemu/icons share/
  mkdir -pv share/qemu/applications
  mv -v share/qemu/applications share/
}

makeinstallpkg() {
  local _pkg=$1

  makeinstall

  echo
  echo "Split installation into partitions"

  cd "${pkgdir}"

  # Start with all content in qemu partition
  local _qemu=qemu/${MINGW_PREFIX}
  mkdir -pv qemu
  mv -v ./${MINGW_PREFIX} qemu/

  # Move to qemu-guest-agent partition
  local _qemu_ga=qemu-guest-agent/${MINGW_PREFIX}
  mkdir -pv ${_qemu_ga}/bin ${_qemu_ga}/share/man/man7 ${_qemu_ga}/share/man/man8
  mv -v ${_qemu}/bin/qemu-ga.exe  ${_qemu_ga}/bin/
  mv -v ${_qemu}/share/man/man8/qemu-ga.8 ${_qemu_ga}/share/man/man8/
  mv -v ${_qemu}/share/man/man7/qemu-ga-ref.7 ${_qemu_ga}/share/man/man7/

  # Move to qemu-image-util partition
  local _qemu_img=qemu-image-util/${MINGW_PREFIX}
  mkdir -pv ${_qemu_img}/bin ${_qemu_img}/share/man/man1
  mv -v ${_qemu}/bin/qemu-img.exe  ${_qemu_img}/bin/
  mv -v ${_qemu}/share/man/man1/qemu-img.1 ${_qemu_img}/share/man/man1/

  # Move to qemu-common partition
  local _qemu_com=qemu-common/${MINGW_PREFIX}
  mkdir -pv ${_qemu_com}/share/doc
  mv -v ${_qemu}/share/doc/qemu  ${_qemu_com}/share/doc/

  # Select requested partition and remove other partitions
  echo
  echo "Select partition ${_pkg} for packaging"
  mv -v ${_pkg}/${MINGW_PREFIX} ./
  rm -rf ${_qemu} ${_qemu_ga} ${_qemu_img} ${_qemu_com}

  cd ./${MINGW_PREFIX}
}

package_qemu() {
  # Split package depends
  depends+=(
    "${MINGW_PACKAGE_PREFIX}-qemu-common"
    "${MINGW_PACKAGE_PREFIX}-qemu-guest-agent"
    "${MINGW_PACKAGE_PREFIX}-qemu-image-util"
  )
  optdepends=(
    "${MINGW_PACKAGE_PREFIX}-gtk-vnc: Contains gvncviewer. For connections to Qemu VNC server"
    "${MINGW_PACKAGE_PREFIX}-spice-gtk: Contains spicy. For connections to Qemu SPICE server"
    "${MINGW_PACKAGE_PREFIX}-virt-viewer: Contains remote-viewer. For connections to Qemu SPICE or VNC server"
  )

  makeinstallpkg qemu

  # Fix firmware descriptors
  # see https://bugzilla.redhat.com/show_bug.cgi?id=1728652#c7
  mkdir -pv share/qemu/firmware
  find share/qemu/firmware -type f -exec \
    sed -i "s%\(\"filename\"\s*:\s*\"\).*edk2%\1${MINGW_PREFIX}/share/qemu/edk2%" {} \;

  # Install tcg plugins to lib/qemu/plugins
  mkdir -pv lib/qemu/plugins/test lib/qemu/plugins/contrib
  install -t lib/qemu/plugins/test "${srcdir}"/build-${MSYSTEM}/tests/tcg/plugins/*.dll
  install -t lib/qemu/plugins/contrib "${srcdir}"/build-${MSYSTEM}/contrib/plugins/*.dll
}

package_qemu-common() {
  depends=()

  makeinstallpkg qemu-common

  # Add all licenses found in qemu sources to share/licences/qemu/
  mkdir -pv share/licenses/qemu
  tar -C "${srcdir}"/${_tarname} -c $(
    cd "${srcdir}"/${_tarname} &&
      find -iname "*COPYING*" -or -iname "*LICENSE*" |
      grep -v "\s" |
      egrep -v "meson.(test|msi|doc)" |
      egrep -v "(license.c|relicense.pl|license.doctree|LicenseCheck)"
    ) | tar -C share/licenses/qemu -xv

  # Add msys2 docs
  install -v -Dm644 -t share/doc/qemu/ "${srcdir}"/msys2.readme.txt
  install -v -Dm644 -t share/doc/qemu/ "${srcdir}"/msys2.qemu-guest-agent.txt
  install -v -Dm755 -t share/doc/qemu/ "${srcdir}"/msys2.examples.tests.sh
}

package_qemu-guest-agent() {
  depends=(
    "${MINGW_PACKAGE_PREFIX}-gettext"
    "${MINGW_PACKAGE_PREFIX}-gcc-libs"
    "${MINGW_PACKAGE_PREFIX}-glib2"
    "${MINGW_PACKAGE_PREFIX}-libc++"
    "${MINGW_PACKAGE_PREFIX}-libiconv"
    "${MINGW_PACKAGE_PREFIX}-libunwind"
    "${MINGW_PACKAGE_PREFIX}-libwinpthread"
  )
  # Split package depends
  depends+=(
    "${MINGW_PACKAGE_PREFIX}-qemu-common"
  )

  makeinstallpkg qemu-guest-agent

  # Upstream doesn't install, but creates qga.msi which contains these
  install -v -Dm644 -t bin/ "${srcdir}"/build-${MSYSTEM}/qga/vss-win32/qga-vss.dll
  install -v -Dm644 -t bin/ "${srcdir}"/build-${MSYSTEM}/qga/vss-win32/qga-vss.tlb
}

package_qemu-image-util() {
  depends=(
    "${MINGW_PACKAGE_PREFIX}-brotli"
    "${MINGW_PACKAGE_PREFIX}-bzip2"
    "${MINGW_PACKAGE_PREFIX}-curl"
    "${MINGW_PACKAGE_PREFIX}-gcc-libs"
    "${MINGW_PACKAGE_PREFIX}-gettext"
    "${MINGW_PACKAGE_PREFIX}-glib2"
    "${MINGW_PACKAGE_PREFIX}-gmp"
    "${MINGW_PACKAGE_PREFIX}-gnutls"
    "${MINGW_PACKAGE_PREFIX}-libiconv"
    "${MINGW_PACKAGE_PREFIX}-libidn2"
    "${MINGW_PACKAGE_PREFIX}-libffi"
    "${MINGW_PACKAGE_PREFIX}-libnfs"
    "${MINGW_PACKAGE_PREFIX}-libpsl"
    "${MINGW_PACKAGE_PREFIX}-libssh"
    "${MINGW_PACKAGE_PREFIX}-libssh2"
    "${MINGW_PACKAGE_PREFIX}-libunistring"
    "${MINGW_PACKAGE_PREFIX}-libtasn1"
    "${MINGW_PACKAGE_PREFIX}-libwinpthread"
    "${MINGW_PACKAGE_PREFIX}-nettle"
    "${MINGW_PACKAGE_PREFIX}-nghttp2"
    "${MINGW_PACKAGE_PREFIX}-openssl"
    "${MINGW_PACKAGE_PREFIX}-p11-kit"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-zstd"
  )
  # Split package depends
  depends+=(
    "${MINGW_PACKAGE_PREFIX}-qemu-common"
  )

  makeinstallpkg qemu-image-util
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
